package VUTHANHPHUC.HOMEWORK.XML;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JSpinner;
import com.toedter.calendar.JDateChooser;
import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class GiaoDienXML extends JFrame {

	private JPanel contentPane;
	private JTextField MaSV;
	private JTextField FullName;
	private JTextField PhoneNo;
	private JTextField Email;
	private JTextField CMND;
	private JTextField KQ;
	private JComboBox GT;
	private JDateChooser Birthday;
	private List<Object> lisField;
	private List<List<Object>> lisValues;
	private List<Object> lisValuField;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GiaoDienXML frame = new GiaoDienXML();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GiaoDienXML() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 554, 457);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblXLTnh = new JLabel("Xử lí tính toán");
		lblXLTnh.setBounds(10, 0, 102, 17);
		contentPane.add(lblXLTnh);

		MaSV = new JTextField();
		MaSV.setBounds(96, 20, 161, 20);
		contentPane.add(MaSV);
		MaSV.setColumns(10);

		JLabel lblNewLabel = new JLabel("Mã Sinh Viên:");
		lblNewLabel.setBounds(9, 20, 77, 21);
		contentPane.add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("Họ Và Tên:");
		lblNewLabel_1.setBounds(10, 60, 60, 20);
		contentPane.add(lblNewLabel_1);

		FullName = new JTextField();
		FullName.setBounds(96, 60, 432, 20);
		contentPane.add(FullName);
		FullName.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("Giới tính:");
		lblNewLabel_2.setBounds(10, 100, 102, 28);
		contentPane.add(lblNewLabel_2);

		GT = new JComboBox();
		GT.setModel(new DefaultComboBoxModel(new String[] { "", "Nam", "Nữ" }));
		GT.setBounds(96, 102, 161, 20);
		contentPane.add(GT);

		JLabel lblNewLabel_3 = new JLabel("Ngày sinh:");
		lblNewLabel_3.setBounds(10, 140, 76, 28);
		contentPane.add(lblNewLabel_3);

		Birthday = new JDateChooser();
		Birthday.setDateFormatString("dd/MM/yyyy");
		Birthday.setBounds(96, 145, 161, 20);
		contentPane.add(Birthday);

		JLabel lblNewLabel_4 = new JLabel("Điện thoại:");
		lblNewLabel_4.setBounds(10, 180, 83, 28);
		contentPane.add(lblNewLabel_4);

		PhoneNo = new JTextField();
		PhoneNo.setBounds(96, 184, 161, 20);
		contentPane.add(PhoneNo);
		PhoneNo.setColumns(10);

		JLabel lblNewLabel_5 = new JLabel("Email:");
		lblNewLabel_5.setBounds(285, 180, 60, 41);
		contentPane.add(lblNewLabel_5);

		Email = new JTextField();
		Email.setBounds(337, 184, 191, 20);
		contentPane.add(Email);
		Email.setColumns(10);

		JLabel lblNewLabel_6 = new JLabel("Số CMND:");
		lblNewLabel_6.setBounds(10, 220, 76, 33);
		contentPane.add(lblNewLabel_6);

		CMND = new JTextField();
		CMND.setBounds(96, 223, 161, 20);
		contentPane.add(CMND);
		CMND.setColumns(10);

		JLabel lblNewLabel_7 = new JLabel("Kết quả:");
		lblNewLabel_7.setBounds(10, 279, 83, 41);
		contentPane.add(lblNewLabel_7);

		KQ = new JTextField();
		KQ.setBounds(96, 263, 432, 84);
		contentPane.add(KQ);
		KQ.setColumns(10);
		
		lisField=new ArrayList<Object>();
		lisField.add("MaSv");
		lisField.add("FullName");
		lisField.add("GT");
		lisField.add("NgaySinh");
		lisField.add("DienThoai");
		lisField.add("Email");
		lisField.add("CMND");
		
		lisValues=new ArrayList<List<Object>>();
		
		
		JButton btnWriteNext = new JButton("Ghi dữ liệu nối tiếp vào file");
		btnWriteNext.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				JFileChooser objFile = new JFileChooser();
                objFile.setCurrentDirectory(new java.io.File("."));
                objFile.setDialogTitle("Chọn file: ");
                objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                String[] EXTENSION = new String[] { "xml" };
                FileNameExtensionFilter filter = new FileNameExtensionFilter("XML files", EXTENSION);
                objFile.setFileFilter(filter);
                objFile.setMultiSelectionEnabled(false);

                int returnVal = objFile.showSaveDialog(getParent());
                String ParentPath = "";
                String FileName = "";
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    ParentPath = objFile.getSelectedFile().getParent();
                    FileName = objFile.getSelectedFile().getName();
                }
                
                Controllers objC1 = new Controllers();
                boolean b;
                try{
                	b=objC1.WriteXML(lisField, lisValues, "School", "Student", ParentPath, FileName, false);
                }catch(Exception e){
                	e.printStackTrace();
                	b=true;
                }
                
                if(b){
                	KQ.setText("Ghi file nối tiếp thành công!!!");
                	lisValues.clear();
                }else{
                	KQ.setText("Ghi file nối tiếp thất bại!");
                }
			
			}
		});
		btnWriteNext.setBounds(96, 374, 147, 33);
		contentPane.add(btnWriteNext);

		JButton btnWriteFile = new JButton("Ghi dữ liệu vào file");
		btnWriteFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser objFile = new JFileChooser();
                objFile.setCurrentDirectory(new java.io.File("."));
                objFile.setDialogTitle("Chọn file: ");
                objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                String[] EXTENSION = new String[] { "xml" };
                FileNameExtensionFilter filter = new FileNameExtensionFilter("XML files", EXTENSION);
                objFile.setFileFilter(filter);
                objFile.setMultiSelectionEnabled(false);

                int returnVal = objFile.showSaveDialog(getParent());
                String ParentPath = "";
                String FileName = "";
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    ParentPath = objFile.getSelectedFile().getParent();
                    FileName = objFile.getSelectedFile().getName();
                }
                
                Controllers objC1 = new Controllers();
                boolean b;
                try{
                	b=objC1.WriteXML(lisField, lisValues, "School", "Student", ParentPath, FileName, true);
                }catch(Exception e){
                	e.printStackTrace();
                	b=false;
                }
                
                if(b){
                	KQ.setText("Ghi file thành công!!!");
                	lisValues.clear();
                }else{
                	KQ.setText("Ghi file thất bại!");
                }
			}
		});
		btnWriteFile.setBounds(269, 374, 117, 33);
		contentPane.add(btnWriteFile);

		JButton btnAddInf = new JButton("Thêm thông tin");
		btnAddInf.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Controllers objC = new Controllers();
				if (MaSV.getText().isEmpty() || FullName.getText().isEmpty()
						|| GT.getSelectedItem().toString().isEmpty()
						|| Birthday.getDate().equals("")
						|| PhoneNo.getText().isEmpty()
						|| Email.getText().isEmpty()
						|| CMND.getText().isEmpty()) {
					JOptionPane.showMessageDialog(getParent(), "Bạn chưa nhập đầy đủ thông tin", "Thông báo", JOptionPane.ERROR_MESSAGE);
				}/*else if(objC.KTEmail(Email.getText())==false){
					JOptionPane.showMessageDialog(getParent(), "Bạn nhập thông tin chưa đúng chuẩn", "Thông báo", JOptionPane.ERROR_MESSAGE);
				}*/else{
					lisValuField=new ArrayList<Object>();
					lisValuField.add(MaSV.getText());
					lisValuField.add(FullName.getText());
					lisValuField.add(GT.getSelectedItem().toString());
					lisValuField.add(new SimpleDateFormat("dd/MM/yyyy").format(Birthday.getDate()));
					lisValuField.add(PhoneNo.getText());
					lisValuField.add(Email.getText());
					lisValuField.add(CMND.getText());
					
					lisValues.add(lisValuField);
					KQ.setText("Có: "+lisValues.size()+" sinh viên trong danh sách");
					
					MaSV.setText("");
					FullName.setText("");
					GT.setSelectedItem(null);
					Birthday.setDate(null);
					PhoneNo.setText("");
					Email.setText("");
					CMND.setText("");
				}
			}
		});
		btnAddInf.setBounds(411, 374, 117, 33);
		contentPane.add(btnAddInf);
	}
}
